package com.lxh.cptp.controller;

import com.alibaba.fastjson.JSONObject;
import com.lxh.cptp.utils.JsonUtils;
import me.chanjar.weixin.cp.bean.WxCpUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.stream.Collectors;

/**
 * created by lanxinghua@2dfire.com on 2020/2/21
 */
@Controller
public class PageController {

    @GetMapping(value = "/")
    public String index(HttpServletRequest request,
                        ModelMap map) {
        HttpSession session = request.getSession();
        JSONObject user = (JSONObject)session.getAttribute("token");
        if (user == null){
            map.put("token", "无token");
            map.put("img", "");
            return "index";
        }
        map.put("token", JsonUtils.toJson(user));
        map.put("img", user.get("avatar").toString());
        return "index";
    }

    /**
     * 域名归属验证
     * @return
     */
    @GetMapping(value = "/WW_verify_EYa97nGhj9n0KaTd.txt")
    public void jsSdkCheck(HttpServletResponse response) throws Exception{
        PrintWriter out = response.getWriter();
        InputStream is = ClassLoader.getSystemResourceAsStream("WW_verify_EYa97nGhj9n0KaTd.txt");
        String result = new BufferedReader(new InputStreamReader(is)).lines().collect(Collectors.joining(System.lineSeparator()));
        out.write(result);
        return;
    }

    @GetMapping(value = "/jssdk")
    public String jsSdk(HttpServletRequest request,
                        ModelMap map) {
        String entityId = request.getParameter("entityId");
        map.put("entityId", StringUtils.isBlank(entityId) ? "测试商圈 000001或者000002" : entityId);
        return "jssdk";
    }


    @GetMapping(value = "/error/404")
    public String error404() {
        return "error";
    }

    @GetMapping(value = "/error/500")
    public String error500() {
        return "error";
    }
}
